package com.sgkusbcamdemo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;

import com.qitianyong.selfclass.BUS_MESSAGE;
import com.qitianyong.selfclass.BaseActivityEvent;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.Message2EventBus;

import de.greenrobot.event.EventBus;

// 此类中需处理事情如下：
// 前后台判断标准

public class BaseActivity extends AppCompatActivity {

    private int __flag = -1;
    private AlertDialog loadingDialog;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }

    public void notifyEmptyBusMessage(int bus_msg) {

        Message2EventBus msg = new Message2EventBus();
        msg.__what = bus_msg;
        EventBus.getDefault().post(msg);
    }

    public void notifyBusMessage(int bus_msg, Object object) {

        Message2EventBus msg = new Message2EventBus();
        msg.__what = bus_msg;
        msg.object = object;
        EventBus.getDefault().post(msg);
    }

    public void onEventMainThread(BaseActivityEvent event) {
        switch (event.event) {
            case BaseActivityEvent.DETACHUSB:
                if (!TextUtils.equals(this.getClass().getName(), MainActivity.class.getName())) {
                    finish();
                } else {
                    notifyEmptyBusMessage(BUS_MESSAGE.BUS_OFFLINE);
                }
                break;
            case BaseActivityEvent.OPENSTREAM:
                if (TextUtils.equals(this.getClass().getName(), MainActivity.class.getName())) {
                    GKDevice.getInstance().openStream();
                }
                break;
        }
    }

    public void showDialog(String text) {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
            loadingDialog.setCancelable(false);
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        loadingDialog.setMessage(text);
        if (!isDestroyed()&&!isFinishing()) {
            loadingDialog.show();
        }
    }
    public void showCancelButtonDialog(String text, DialogInterface.OnClickListener onClickListener) {
            loadingDialog = new ProgressDialog(this);
            loadingDialog.setCancelable(false);
            loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setMessage(text);
        loadingDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",onClickListener);
        if (!isDestroyed()&&!isFinishing()&&!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }
    public void showDialog() {
        showDialog("");
    }
    public void cancelDialog(){
        if(loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
    }
    public void cancelCancelButtonDialog(){
        if(loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
        loadingDialog =null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
    protected void toast(String message){
        if(toast != null){
            toast.cancel();
        }
        toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.show();
    }
}
